package com.chenliang.provider.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: springboot_rabbitmq_all
 * @description:
 * @author: 陈亮
 * @create: 2022-05-06 21:26
 */

/**
 * 这个交换机配置类在生产者,生产者声明交换机和队列,交换机和队列的关系,
 * 生产者启动的时候,控制台不会生成交换机和队列,
 * 生产者服务在发送消息的时候,如果生产者服务有构建队列和交换机bean的话,控制台就会有队列和交换机
 */
@Configuration
public class RabbitMqBeanConfig {


    public static final String TOPIC_EXCHANGE_NAME = "providerExchange";

    public static final String TOPIC_USER_QUEUE_NAME = "providerQueueWithKeyUser";
    public static final String TOPIC_GOODS_QUEUE_NAME = "providerQueueWithKeyGoods";

    /**
     * 声明交换机
     */
    @Bean("chenLiangTopicExchange2")
    public Exchange exchange() {
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE_NAME).durable(true).build();
    }

    /**
     * 声明队列
     */
    @Bean("chenLiangTopicQueue2")
    public Queue queue() {
        return QueueBuilder.durable(TOPIC_USER_QUEUE_NAME).build();
    }

    @Bean("chenLiangTopicQueue3")
    public Queue queue1() {
        return QueueBuilder.durable(TOPIC_GOODS_QUEUE_NAME).build();
    }

    /**
     * 绑定交换机和队列的关系
     */
    @Bean
    public Binding binding(@Qualifier("chenLiangTopicExchange2") Exchange exchange, @Qualifier("chenLiangTopicQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("user.*").noargs();
    }

    /**
     * 绑定交换机和队列的关系
     */
    @Bean
    public Binding binding1(@Qualifier("chenLiangTopicExchange2") Exchange exchange, @Qualifier("chenLiangTopicQueue3") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("goods.*").noargs();
    }
}
